﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Trade.DAL;
using Trade.Model;
using Trade.Controls;
using Trade;

namespace Trade
{
    public partial class FrmSubjectMessage : RayForm
    {
        private SubjectMessage subjectMessage = new SubjectMessage();
        private Utility.CRUD dataState = Utility.CRUD.None;
        private Common common = new Common();
        //private IList<SubjectInfo> subject = null;

        public FrmSubjectMessage()
        {
            InitializeComponent();
            common.SetControlsState(this, false);
            cmbDisabled.SelectedIndex = 0;
            cmbDetailFlag.SelectedIndex = 0;
            dgvSubjectMessage.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            btnOperatorGroup.btnNew.Click +=new EventHandler(btnNew_Click);
            btnOperatorGroup.btnModify.Click += new EventHandler(btnModify_Click);
            btnOperatorGroup.btnDelete.Click += new EventHandler(btnDelete_Click);
            btnOperatorGroup.btnSave.Click += new EventHandler(btnSave_Click);
            btnOperatorGroup.btnCancel.Click += new EventHandler(btnCancel_Click);
            btnOperatorGroup.btnQuit.Click += new EventHandler(btnQuit_Click);
        }

        //退出
        private void btnQuit_Click(object sender, EventArgs e)
        {
            if (dataState != Utility.CRUD.None)
            {
                if (MessageBox.Show("当前数据还未保存,是否需要保存数据?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)  == System.Windows.Forms.DialogResult.Yes)
                {
                    btnOperatorGroup.btnSave.Focus();
                    return;
                }
            }

            this.Close();
        }

        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.None;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            RefreshDetailControls();
            common.SetControlsState(this, false);
            SetDataViewerStatus(true);
            tvwSubject.Focus();
        }

        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            IList<SubjectInfo> subject = (IList<SubjectInfo>)dgvSubjectMessage.DataSource;
            SubjectInfo si = new SubjectInfo();

            si.SubjectId = dataState != Utility.CRUD.Insert ? subject[dgvSubjectMessage.CurrentRow.Index].SubjectId : 0; 
            si.SubjectCode = txtSubjectCode.Text.Trim();
            si.SubjectName = txtSubjectName.Text.Trim();
            si.ParentId = Convert.ToInt32(tvwSubject.SelectedNode.Tag);
            si.Disabled = cmbDisabled.SelectedIndex == 0 ? true : false;
            si.DetailFlag = cmbDetailFlag.SelectedIndex == 0 ? true : false;
            si.Description = txtDescription.Text.Trim();

            subjectMessage.WriteToDatabase(Utility.CRUDAction(dataState), si);
            /*
            switch (dataState)
            { 
                case "A":
                    break;
                case "C":
                    break;
                case "D":
                    break;
            }*/

            dataState = Utility.CRUD.None;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            dgvSubjectMessageBindData();
            RefreshDetailControls();
            SetDataViewerStatus(true);
            common.SetControlsState(this, false);
            tvwSubject.Focus();
        }

        //删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(tvwSubject.SelectedNode.Tag) != 0)
            {
                if (MessageBox.Show("是否真的删除当前数据?", "删除数据", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                {
                    dataState = Utility.CRUD.Delete;
                    common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
                }
            }
            else
            {
                MessageBox.Show("父项科目不能删除!", "删除数据", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //编辑
        private void btnModify_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.Update;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);

            common.SetControlsState(this, true);
            txtSubjectCode.Enabled = false;
            txtSubjectName.Focus();
        }

        //新增
        private void btnNew_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.Insert;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);

            if (Convert.ToInt32(tvwSubject.SelectedNode.Tag) != 0)
            {
                int positionStart = tvwSubject.SelectedNode.Text.IndexOf("[") + 1;
                int positionEnd = tvwSubject.SelectedNode.Text.IndexOf("]");

                common.SetControlsState(this, true);
                txtSubjectCode.Text = subjectMessage.GenNewSubjectCode(tvwSubject.SelectedNode.Text.Substring(positionStart, positionEnd - positionStart).Trim());
                txtSubjectCode.Enabled = false;
                SetDataViewerStatus(false);
                txtSubjectName.Focus();
                txtSubjectName.Text = "";
                txtDescription.Text = "";
            }
        }

        private void SubjectMessageForm_Load(object sender, EventArgs e)
        {
            IList<SubjectInfo> subject = subjectMessage.GetSubjectByParentId(0);

            tvwSubject.BeginUpdate();
            tvwSubject.Nodes.Add("科目资料");
            tvwSubject.Nodes[0].Tag = "0";

            int i = 0;

            foreach (SubjectInfo subjectItem in subject)
            {
                tvwSubject.Nodes[0].Nodes.Add("[ " + subjectItem.SubjectCode.Trim() + " ] " + subjectItem.SubjectName.Trim());
                tvwSubject.Nodes[0].Nodes[i].Tag = subjectItem.SubjectId.ToString();
                i++;
            }

            tvwSubject.SelectedNode = tvwSubject.Nodes[0];
            tvwSubject.SelectedNode.ExpandAll();
            tvwSubject.Focus();
            tvwSubject.EndUpdate();

            //dgvSubjectMessage.Focus();
        }

        private void tvwSuject_AfterSelect(object sender, TreeViewEventArgs e)
        {
            dgvSubjectMessageBindData();
        }

        private void dgvSubjectMessageBindData()
        {
            int subjectId = Convert.ToInt32(tvwSubject.SelectedNode.Tag);
            IList<SubjectInfo> subject = subjectMessage.GetSubjectByParentId(subjectId);

            dgvSubjectMessage.DataSource = subject;

            dgvSubjectMessage.Columns[0].Width = 80;
            dgvSubjectMessage.Columns[0].HeaderText = "科目ID";
            dgvSubjectMessage.Columns[0].Visible = false;
            dgvSubjectMessage.Columns[1].Width = 100;
            dgvSubjectMessage.Columns[1].HeaderText = "科目编码";
            dgvSubjectMessage.Columns[2].Width = 150;
            dgvSubjectMessage.Columns[2].HeaderText = "科目名称";
            dgvSubjectMessage.Columns[3].Width = 80;
            dgvSubjectMessage.Columns[3].HeaderText = "父项ID";
            dgvSubjectMessage.Columns[3].Visible = false;
            dgvSubjectMessage.Columns[4].Width = 80;
            dgvSubjectMessage.Columns[4].HeaderText = "不可使用";
            dgvSubjectMessage.Columns[4].Visible = false;
            dgvSubjectMessage.Columns[5].Width = 80;
            dgvSubjectMessage.Columns[5].HeaderText = "明细标识";
            dgvSubjectMessage.Columns[5].Visible = false;
            dgvSubjectMessage.Columns[6].Width = 200;
            dgvSubjectMessage.Columns[6].HeaderText = "描述";
        }

        //设置数据浏览控件的Enabled
        private void SetDataViewerStatus(bool status)
        {
            tvwSubject.Enabled = status;
            dgvSubjectMessage.Enabled = status;
        }

        //刷新控件数据
        private void RefreshDetailControls()
        {
            IList<SubjectInfo> subject = (IList<SubjectInfo>)dgvSubjectMessage.DataSource;
            if (dgvSubjectMessage.CurrentRow != null)
            {
                txtSubjectCode.Text = subject[dgvSubjectMessage.CurrentRow.Index].SubjectCode;
                txtSubjectName.Text = subject[dgvSubjectMessage.CurrentRow.Index].SubjectName;
                txtDescription.Text = subject[dgvSubjectMessage.CurrentRow.Index].Description;
                cmbDisabled.SelectedIndex = subject[dgvSubjectMessage.CurrentRow.Index].Disabled ? 0 : 1;
                cmbDetailFlag.SelectedIndex = subject[dgvSubjectMessage.CurrentRow.Index].DetailFlag ? 0 : 1;
            }
        }

        private void dgvSubjectMessage_SelectionChanged(object sender, EventArgs e)
        {
            RefreshDetailControls();
        }
    }
}
